home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Graphics⁄Sound / Fudd Source / Fudd ƒ / Fudd code ƒ / fudd.c < prev    next >
Text File  |  1994-02-06  |  5KB  |  202 lines

  1. /**********************************************************************\
  2.  
  3. File:        fudd.c
  4.  
  5. Purpose:    This module handles Fudd-specific initialization and
  6.             shutdown, memory setup for converting, and a dispatch
  7.             for file conversion.
  8.  
  9.  
  10. Fudd -=- convert text to Elmer Fudd talk
  11. Copyright ©1994, Mark Pilgrim
  12.  
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; either version 2 of the License, or
  16. (at your option) any later version.
  17.  
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. GNU General Public License for more details.
  22.  
  23. You should have received a copy of the GNU General Public License
  24. along with this program in a file named "GNU General Public License".
  25. If not, write to the Free Software Foundation, 675 Mass Ave,
  26. Cambridge, MA 02139, USA.
  27.  
  28. \**********************************************************************/
  29.  
  30. #include "fudd.h"
  31. #include "fudd meat.h"
  32. #include "fudd files.h"
  33. #include "fudd file management.h"
  34. #include "fudd error.h"
  35. #include "fudd progress.h"
  36. #include "program globals.h"
  37. #include "util.h"
  38. #include "msg environment.h"
  39. #include "msg menus.h"
  40.  
  41. unsigned char    gShowSaveDialog;
  42. unsigned char    gAddSuffix;
  43. unsigned char    gShowProgress;
  44.  
  45. void InitProgram(void)
  46. {
  47.     int                i,j,k;
  48.     AppFile            myFile;
  49.     
  50.     CountAppFiles(&i, &j);
  51.     if ((j>0) && (i==0))
  52.     {
  53.         for (k=1; k<=j; k++)
  54.         {
  55.             GetAppFiles(1, &myFile);
  56.             MyMakeFSSpec(myFile.vRefNum, 0, myFile.fName, &inputFS);
  57.             StartConvert();
  58.             ClrAppFiles(k);
  59.         }
  60.     }
  61. }
  62.  
  63. void NewConvert(void)
  64. {
  65.     inputFS.name[0]=outputFS.name[0]=tempFS.name[0]=0x00;
  66.     if (GetSourceFile(&inputFS, FALSE))
  67.         StartConvert();
  68. }
  69.  
  70. void StartConvert(void)
  71. {
  72.     int                err;
  73.     Boolean            notDoneYet;
  74.     
  75.     outputFS=inputFS;
  76.     if (gAddSuffix)
  77.     {
  78.         if (outputFS.name[0]>28)
  79.             outputFS.name[0]=28;
  80.         outputFS.name[++(outputFS.name[0])]='.';
  81.         outputFS.name[++(outputFS.name[0])]='f';
  82.         outputFS.name[++(outputFS.name[0])]='u';
  83.         outputFS.name[++(outputFS.name[0])]='d';
  84.         outputFS.name[++(outputFS.name[0])]='d';
  85.     }
  86.     deleteTheThing=TRUE;
  87.     
  88.     if (gShowSaveDialog)
  89.         if (!GetDestFile(&outputFS, &deleteTheThing))
  90.             return;
  91.  
  92.     gInputNeedsUpdate=TRUE;
  93.     gOutputNeedsUpdate=FALSE;
  94.     gInputOffset=gOutputOffset=gAbsoluteOffset=0L;
  95.     
  96.     InitFiles();
  97.     HandleError(err=OpenInputFile());
  98.     if (err!=allsWell)
  99.         return;
  100.     HandleError(err=CreateTempFile());
  101.     if (err!=allsWell)
  102.     {
  103.         FinalizeFiles(FALSE);
  104.         return;
  105.     }
  106.     HandleError(err=SetupTempFile());
  107.     if (err!=allsWell)
  108.     {
  109.         FinalizeFiles(FALSE);
  110.         return;
  111.     }
  112.     
  113.     gInputBuffer=NewPtrClear(INPUT_BUFFER_MAX+16);
  114.     if (gInputBuffer==0L)
  115.     {
  116.         FinalizeFiles(FALSE);
  117.         HandleError(kNoMemory);
  118.         return;
  119.     }
  120.     
  121.     gOutputBuffer=NewPtrClear(OUTPUT_BUFFER_MAX+16);
  122.     if (gOutputBuffer==0L)
  123.     {
  124.         FinalizeFiles(FALSE);
  125.         DisposePtr(gInputBuffer);
  126.         HandleError(kNoMemory);
  127.         return;
  128.     }
  129.     
  130.     gInProgress=TRUE;
  131.     AdjustMenus();
  132.     DrawMenuBar();
  133.     
  134.     if (showThatTharProgress)
  135.     {
  136.         SetProgressText("\pConverting ",inputFS.name, "\p...","\p");
  137.         OpenProgressDialog(gInputLength, "\pFudd");
  138.     }
  139.     
  140.     notDoneYet=TRUE;
  141.     err=allsWell;
  142.     while ((notDoneYet) && (err==allsWell) && (gAbsoluteOffset<gInputLength))
  143.     {
  144.         if (gInputNeedsUpdate)
  145.         {
  146.             if (gInputOffset>0L)
  147.                 ShiftInputBuffer();
  148.             HandleError(err=ReadInputFile(inputRefNum, gInputBuffer+
  149.                 gWhatsReallyInInputBuffer, INPUT_BUFFER_MAX+16-gWhatsReallyInInputBuffer));
  150.             gInputNeedsUpdate=FALSE;
  151.             if (showThatTharProgress)
  152.             {
  153.                 UpdateProgressDialog(gAbsoluteOffset);
  154.                 notDoneYet=DealWithOtherPeople();
  155.             }
  156.         }
  157.         
  158.         if ((notDoneYet) && (err==allsWell) && (gOutputNeedsUpdate))
  159.         {
  160.             HandleError(err=WriteTempFile(outputRefNum, gOutputBuffer, gOutputOffset));
  161.             gOutputOffset=0L;
  162.             gOutputNeedsUpdate=FALSE;
  163.         }
  164.         
  165.         if ((notDoneYet) && (err==allsWell))
  166.             ConvertIt();
  167.     }
  168.     
  169.     if (showThatTharProgress)
  170.     {
  171.         UpdateProgressDialog(gAbsoluteOffset);
  172.         DealWithOtherPeople();
  173.     }
  174.     
  175.     if (gOutputOffset>0L)
  176.     {
  177.         HandleError(err=WriteTempFile(outputRefNum, gOutputBuffer, gOutputOffset));
  178.     }
  179.     
  180.     DisposePtr(gInputBuffer);
  181.     DisposePtr(gOutputBuffer);
  182.     
  183.     FinalizeFiles((notDoneYet) && (err==allsWell));
  184.     
  185.     if (showThatTharProgress)
  186.         DismissProgressDialog();
  187.     gInProgress=FALSE;
  188.     AdjustMenus();
  189.     DrawMenuBar();
  190. }
  191.  
  192. void ShiftInputBuffer(void)
  193. {
  194.     Mymemcpy(gInputBuffer, gInputBuffer+gInputOffset, INPUT_BUFFER_MAX+16-gInputOffset);
  195.     gWhatsReallyInInputBuffer=INPUT_BUFFER_MAX+16-gInputOffset;
  196.     gInputOffset=0L;
  197. }
  198.  
  199. void ShutDownProgram(void)
  200. {
  201. }
  202.